package asia.doing.controller.front;

import asia.doing.common.Result;
import asia.doing.entity.Article;
import asia.doing.service.front.ArticleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "文章管理" ,description = "页面展示管理系统文章相关接口")
@RestController(value = "frontArticleController")
@RequestMapping("/v1/front/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @ApiOperation(value = "id查询文章")
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Article article = new Article();
        article.setId(id);
        List<Article> articles = articleService.selectArticle(article);
        return Result.success(articles.get(0));
    }


    @ApiOperation(value = "增加文章浏览数")
    @PostMapping("/views/{id}")
    public Result addViews(@PathVariable(value = "id") Integer id) {
        System.out.println("获取到id" + id);
        articleService.addViews(id);
        return Result.success("浏览数增加成功");
    }

    /**
     * 查询所有的文章
     * @param article 这里分页要传递文章id
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "文章分页")
    @GetMapping
    public Result selectPage(Article article,
                             @RequestParam(required = false) Integer pageNum,
                             @RequestParam(required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            // 如果 pageNum 或 pageSize 为 null，则查询所有数据
            List<Article> allArticles = articleService.selectArticle(article);
            return Result.success(allArticles);
        } else {
            // 执行分页查询
            PageInfo<Article> page = articleService.selectPage(article, pageNum, pageSize);
            return Result.success(page);
        }
    }
}
